package com.itheima.mall.web.pms;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.commons.result.Result;
import com.itheima.mall.dubbo.api.pms.PmsBrandService;
import com.itheima.mall.entity.domain.pms.Brand;
import com.itheima.mall.entity.page.MyPage;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class PmsBrandController {
    @DubboReference
    private PmsBrandService brandService;
    //添加商品
    @PostMapping("create")
    public Result create(@RequestBody Brand brand){
        brandService.save(brand);
        return Result.success("添加成功");
    }
    //批量删除品牌
    @GetMapping("/delete/batch")
    public Result delete(@RequestParam List<Integer> ids){
        for (Integer id : ids) {
            brandService.removeById(id);
        }
        //brandService.removeByIds(ids);
        return Result.success("删除成功");
    }
    //批量删除品牌
    @GetMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        brandService.removeById(id);
        return Result.success("删除成功");
    }
    //根据品牌名称分页获取品牌列表
    @GetMapping("list")
    public Result list(String keyword,Integer pageNum,Integer pageSize){
       MyPage<Brand> brandMyPage =  brandService.getList(keyword,pageNum,pageSize);
       return Result.success(brandMyPage);
    }
    //获取全部品牌列表
    @GetMapping("listAll")
    public Result listAll(){
        List<Brand> list = brandService.list();
        return Result.success(list);
    }
    //批量更新厂家制造商状态
    @PostMapping("/update/FactoryStatus")
    public Result updateFactoryStatus(@RequestParam Integer factoryStatus,@RequestParam List<Long> ids){
        List<Brand> brands = brandService.listByIds(ids);
        for (Brand brand : brands) {
            brand.setFactoryStatus(factoryStatus);
            brandService.updateById(brand);
        }
        return Result.success("修改成功");
    }
    //批量更新显示状态
    @PostMapping("/update/showStatus")
    public Result updateShowStatus(@RequestParam Integer showStatus,@RequestParam List<Long> ids){
        List<Brand> brands = brandService.listByIds(ids);
        for (Brand brand : brands) {
            brand.setShowStatus(showStatus);
            brandService.updateById(brand);
        }
        return Result.success("修改成功");
    }
    //更新品牌
    @PostMapping("/update/{id}")
    public Result update(@RequestParam Long id,@RequestBody Brand brand){
        brand.setId(id);
        brandService.updateById(brand);
        return Result.success("修改成功");
    }
    //根据编号查询品牌信息
    @GetMapping("/{id}")
    public Result getById(Long id){
        Brand brand = brandService.getById(id);
        return Result.success(brand);
    }
}
